home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / copymask / copymask.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.0 KB  |  109 lines

  1. /*
  2.     File:        CopyMask.c
  3.  
  4.     Contains:    Shows how CopyMask can used to fade a screen to a lighter color.    
  5.  
  6.     Written by: John Wang    
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include     <Dialogs.h>
  25. #include    <Fonts.h>
  26. #include     <Processes.h>
  27. #include    <QDOffscreen.h>
  28. #include    <Sound.h>
  29.  
  30. void main(void)
  31. {
  32.     WindowPtr        mainWinPtr;
  33.     OSErr            error;
  34.     SysEnvRec        theWorld;
  35.     GWorldPtr        myOffscreen1, myOffscreen2;
  36.     GDHandle        oldDevice;
  37.     CGrafPtr        oldPort;
  38.     Rect            windRect, offRect, myRect;
  39.     RGBColor        theColor;
  40.     int                i;
  41.     
  42.     /* Make sure ColorQD exists. */
  43.     error = SysEnvirons(1, &theWorld);
  44.     if (theWorld.hasColorQD == false) {
  45.         SysBeep(50);
  46.         ExitToShell();
  47.     }
  48.     
  49.     /* Initialize all the needed managers. */
  50.     InitGraf(&qd.thePort);
  51.     InitFonts();
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(nil);
  56.     InitCursor();
  57.  
  58.     /* Define output window. */
  59.     SetRect(&windRect, 100, 100, 400, 400);
  60.     SetRect(&offRect, 0, 0, 300, 300);
  61.     mainWinPtr = NewCWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
  62.     SetPort(mainWinPtr);
  63.     GetGWorld(&oldPort, &oldDevice);
  64.  
  65.     /* Create two offscreen GWorlds. */
  66.     if (NewGWorld(&myOffscreen1, 0, &offRect, nil, nil, 0))
  67.         Debugger();
  68.     if (NewGWorld(&myOffscreen2, 0, &offRect, nil, nil, 0))
  69.         Debugger();
  70.     
  71.     /* Now, draw ramp to first offscreen pixmap. */
  72.     SetGWorld(myOffscreen1, nil);
  73.     for (i=0; i<10; i++) {
  74.         theColor.red = theColor.green = theColor.blue = (i*28) << 8;
  75.         RGBForeColor(&theColor);
  76.         SetRect(&myRect, 0, i*30, 300, i*30+30);
  77.         PaintRect(&myRect);
  78.     }
  79.  
  80.     /* Now, draw mask to second offscreen pixmap. */
  81.     SetGWorld(myOffscreen2, nil);
  82.     SetRect(&myRect, 0, 0, 300, 300);
  83.     theColor.red = theColor.green = theColor.blue = (128) << 8;
  84.     RGBForeColor(&theColor);
  85.     PaintRect(&myRect);
  86.  
  87.     /*    Draw to the ramp to the window.    */
  88.     SetGWorld(oldPort, oldDevice);
  89.     CopyBits((BitMapPtr) *myOffscreen1->portPixMap, &(*mainWinPtr).portBits, &offRect,
  90.                 &offRect, 0, nil);
  91.  
  92.     /* Wait until user clicks button. */
  93.     do {
  94.     } while (!Button());
  95.     
  96.     /*    Now fade the window to white using CopyMask.  The destination must be erased
  97.         for CopyMask to work.    */
  98.     SetGWorld(oldPort, oldDevice);
  99.     EraseRect(&offRect);
  100.     CopyMask((BitMapPtr) *myOffscreen1->portPixMap, (BitMapPtr) *myOffscreen2->portPixMap,
  101.                 &(*mainWinPtr).portBits, &offRect, &offRect, &offRect);
  102.  
  103.     /* Wait until user clicks button. */
  104.     do {
  105.     } while (Button());
  106.     do {
  107.     } while (!Button());
  108. }
  109.